package com.rest.demo.Service;

import com.alibaba.fastjson.JSONObject;
import com.rest.demo.util.restUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Data
@Service
@Slf4j
@Component
@Configuration
@RestController
public class Dysmtl2 {

    public String result;
    @Value("${ServerSC.MainUrl}")
    public String MainUrl;//请求code的主地址


    private RestTemplate restTemplate = new RestTemplate();

    private restUtil XRestUtil = new restUtil();


    public String UrlCode(String Qc) throws UnsupportedEncodingException {

        String encode = URLEncoder.encode(Qc, "utf-8");
        System.out.println("乱码" + encode);

        String decode = URLDecoder.decode(encode, "utf-8");// GBK解码
        System.out.println(decode);
        return encode;
    }






    //apiToken="EUTUFTGKSNPMVERX"
    //Get直接获取Code
    //Scope:snsapi_base 只包括用户id,用户名
    //Scope:snsapi_baseinfo 用户基本信息
    //Scope:snsapi_userinfo 用户基本信息和敏感信息(身份证,认证资料,资料库等)
    public String GetCode(String Name,String gszchm2, String appCode, String jgdm, String apiToken, String DysmtGsUrl) throws IOException {


        Map<String, Object> tyshxydm = new HashMap<String, Object>();
        tyshxydm.put("bm", "dwmc");
        tyshxydm.put("jtz", Name);
        Map<String, Object> qymc_jg = new HashMap<String, Object>();
        qymc_jg.put("bm", "tyshxydm");
        qymc_jg.put("jtz", jgdm);
        Map<String, Object> gszchm = new HashMap<String, Object>();
        gszchm.put("bm", "gszchm");
        gszchm.put("jtz", gszchm2);

        List list = new ArrayList();
        list.add(tyshxydm);
        list.add(qymc_jg);
        list.add(gszchm);

        Map<String, Object> query = new HashMap<String, Object>();
        query.put("apiToken", apiToken);

        query.put("pageIndex", "1");
        query.put("pageSize", "10");
        query.put("list", list);

        String JsonString = JSONObject.toJSONString(query);//map对象转Json字符串
        System.out.println(JsonString);
//        JSONObject.toJSONString(query);

        String query2 = UrlCode(JsonString);

        Map<String, Object> GetRep = new HashMap<String, Object>();
        GetRep.put("appCode", appCode);//appCode动态码获取
        GetRep.put("query", JsonString);//apitoken及相关参数集合
        System.out.println(GetRep);

        try {
            result = restTemplate.getForObject(DysmtGsUrl + "?appCode={appCode}" + "&query={query}", String.class, GetRep);
            System.out.println(result);

        } catch (Exception e) {
            log.error(e.getMessage(), e);

        }


        return result;


    }





    //apiToken="EUTUFTGKSNPMVERX"
    //Get直接获取Code
    //Scope:snsapi_base 只包括用户id,用户名
    //Scope:snsapi_baseinfo 用户基本信息
    //Scope:snsapi_userinfo 用户基本信息和敏感信息(身份证,认证资料,资料库等)
    public String GetYcmlInfo(String Name, String Dm, String appCode,String apiToken, String ReqURl) throws IOException {


        Map<String, Object> tyshxydm = new HashMap<String, Object>();
        tyshxydm.put("bm", "qyjgmc");
        tyshxydm.put("jtz", Name);
        Map<String, Object> qymc_jg = new HashMap<String, Object>();
        qymc_jg.put("bm", "tyshxydm");
        qymc_jg.put("jtz", Dm);

System.out.println(qymc_jg);
        List list = new ArrayList();
        list.add(tyshxydm);
        list.add(qymc_jg);


        Map<String, Object> query = new HashMap<String, Object>();
        query.put("apiToken", apiToken);
        query.put("pageIndex", "1");
        query.put("pageSize", "10");
        query.put("list", list);

        String JsonString = JSONObject.toJSONString(query);//map对象转Json字符串
//        System.out.println(JsonString);



        Map<String, Object> GetRep = new HashMap<String, Object>();
        GetRep.put("appCode", appCode);//appCode动态码获取
        GetRep.put("query", JsonString);//apitoken及相关参数集合

        System.out.println(ReqURl + "?appCode="+appCode+"&query="+JsonString);




        try {
            result = restTemplate.getForObject(ReqURl + "?appCode={appCode}" + "&query={query}", String.class, GetRep);
//          System.out.println(result);

        } catch (Exception e) {
            log.error(e.getMessage(), e);

        }


        return result;


    }




    public String GetCode2(String Url) {
        System.out.println(restTemplate);

        JSONObject jsonObject = JSONObject.parseObject(restTemplate.getForObject(Url, String.class));


        return jsonObject.getString("data");

    }


}

